package com.nyj.dao.impl;

import com.nyj.dao.IUserDao;
import com.nyj.io.Resources;
import com.nyj.pojo.User;
import com.nyj.sqlSession.SqlSession;
import com.nyj.sqlSession.SqlSessionFactory;
import com.nyj.sqlSession.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

/**
 * @author niuyj
 * @date 2020/5/3 14:02
 * @description
 */
public class UserDaoImpl implements IUserDao {
    @Override
    public List<User> findAll() {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = new User();
        user.setId(1);
        user.setUsername("lucy");

        List<User> objects = sqlSession.selectList("user.selectList");
        System.out.println("user.selectList:"+objects);
        return objects;
    }

    @Override
    public User findByCondition(User user) {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();


        User user1 = sqlSession.selectOne("user.selectOne", user);
        System.out.println("user.selectOne:"+user1);


        return user1;
    }
}
